#ifndef MYMOVIERESPONSEPARSE_H
#define MYMOVIERESPONSEPARSE_H

#include <QXmlDefaultHandler>
#include <QDebug>
#include "discdata.h"
#include "moviedata.h"
#include <QList>


class MyMovieResponseParse : public QXmlDefaultHandler
{
public:
    MyMovieResponseParse();
    bool startDocument()
    {
        return true;
    }
    bool endElement( const QString& val1, const QString& val2, const QString &name )
    {
        qDebug() << "endElement: ";

        qDebug() << "val1: "<<val1;
        qDebug() << "val2: "<<val2;
        qDebug() << "name: "<<name;
        return true;
    }

    bool startElement( const QString& namespaceURI, const QString& localName, const QString &qName, const QXmlAttributes &attrs  )
    {
        qDebug() << "startElement";
        qDebug() << "name:" + qName;
        qDebug() << "localName:" + localName;
        qDebug() << "namespaceURI:" + namespaceURI;

        if(qName == "Title")
        {
            DiscData discData;
            MovieData movieData;            
            for( int i=0; i<attrs.count(); i++ )
            {
                qDebug()<<"attrs "<<i<<" :"<< attrs.value( i );
                if(attrs.type(i ) == "CDATA")
                {
                    if(attrs.qName( i )=="id")
                    {
                        movieData.id=attrs.value( i );
                        discData.setId(attrs.value( i ));
                    }
                    else if(attrs.qName( i )=="type")
                    {
                        discData.setType(attrs.value( i ));
                        movieData.type=attrs.value( i );
                    }
                    else if(attrs.qName( i )=="country")
                    {
                        discData.setCountry(attrs.value( i ));
                        movieData.country=attrs.value( i );
                    }
                    else if(attrs.qName( i )=="barcode")
                    {
                        discData.barcode = attrs.value( i );
                        movieData.barcode=attrs.value( i );
                    }
                    else if(attrs.qName( i )=="title")
                    {
                        discData.setTitle(attrs.value( i ));
                        movieData.title=attrs.value( i );
                    }
                    else if(attrs.qName( i )=="originalTitle")
                    {
                        discData.originalTitle = attrs.value( i );
                        movieData.originalTitle=attrs.value( i );
                    }
                    else if(attrs.qName( i )=="edition")
                    {
                        discData.edition = attrs.value( i );
                        movieData.edition=attrs.value( i );
                    }
                    else if(attrs.qName( i )=="year")
                    {
                        discData.year = attrs.value( i );
                        movieData.year=attrs.value( i );
                    }
                    else if(attrs.qName( i )=="imdb")
                    {
                        discData.imdb = attrs.value( i );
                        movieData.imdb=attrs.value( i );
                    }
                    else if(attrs.qName( i )=="thumbnail")
                    {
                        discData.thumbnail = attrs.value( i );
                        movieData.thumbnail=attrs.value( i );
                    }
                    else if(attrs.qName( i )=="thumbnailwidth")
                    {
                        discData.thumbnailwidth = attrs.value( i );
                        movieData.thumbnailwidth=attrs.value( i );
                    }
                    else if(attrs.qName( i )=="thumbnailheight")
                    {
                        discData.thumbnailheight = attrs.value( i );
                        movieData.thumbnailheight=attrs.value( i );
                    }
                    else if(attrs.qName( i )=="bigthumbnail")
                    {
                        discData.bigthumbnail = attrs.value( i );
                        movieData.bigthumbnail=attrs.value( i );
                    }
                    else if(attrs.qName( i )=="bigthumbnailwidth")
                    {
                        discData.bigthumbnailwidth = attrs.value( i );
                        movieData.bigthumbnailwidth=attrs.value( i );
                    }
                    else if(attrs.qName( i )=="bigthumbnailheight")
                    {
                        discData.bigthumbnailheight = attrs.value( i );
                        movieData.bigthumbnailheight=attrs.value( i );
                    }
                    else if(attrs.qName( i )=="completepercentage")
                    {
                        discData.completepercentage = attrs.value( i );
                        movieData.completepercentage=attrs.value( i );
                    }
                    else if(attrs.qName( i )=="parentalRating")
                    {
                        discData.parentalRating = attrs.value( i );
                        movieData.parentalRating=attrs.value( i );
                    }
                    else if(attrs.qName( i )=="score")
                    {
                        discData.score = attrs.value( i );
                        movieData.score=attrs.value( i );
                    }
                    else if(attrs.qName( i )=="IsBoxSetParent")
                    {
                        discData.isBoxSetParent = attrs.value( i );
                        movieData.isBoxSetParent=attrs.value( i );
                    }
                    else if(attrs.qName( i )=="IsDiscSetChild")
                    {
                        discData.isDiscSetChild = attrs.value( i );
                        movieData.isDiscSetChild=attrs.value( i );
                    }
                    else if(attrs.qName( i )=="IsBoxSetConfirmed")
                    {
                        discData.isBoxSetConfirmed = attrs.value( i );
                        movieData.isBoxSetConfirmed=attrs.value( i );
                    }
                    else
                    {
                        qDebug()<<"Unknow case:"<<attrs.qName( i )<<" value:"<<attrs.value( i );
                        break;

                    }
                }
            }
            _movieDataList<<movieData;
            _discDataList<<discData;
        }
        return true;
    }
    QList<DiscData> getDiscDataList() const;
    QList<MovieData> getMovieDataList() const;
private:
    QList<DiscData> _discDataList;
    QList<MovieData> _movieDataList;
};

#endif // MYMOVIERESPONSEPARSE_H
